package cn.tedu.blackhorse.service.impl;

import cn.tedu.blackhorse.ex.ServiceCode;
import cn.tedu.blackhorse.ex.ServiceException;
import cn.tedu.blackhorse.mapper.ClassMapper;
import cn.tedu.blackhorse.mapper.StudentMapper;
import cn.tedu.blackhorse.pojo.dto.ClassAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.ClassUpdateDTO;
import cn.tedu.blackhorse.pojo.entity.Class;
import cn.tedu.blackhorse.pojo.vo.ClassListItemVO;
import cn.tedu.blackhorse.pojo.vo.ClassStanardVO;
import cn.tedu.blackhorse.service.IClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ClassServiceImpl implements IClassService {

    @Autowired
    private ClassMapper mapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void insertClass(ClassAddNewDTO classAddNewDTO) {
        int count = mapper.countByName(classAddNewDTO.getClassName());
        if (count != 0){
            String message = "添加失败,添加班级已存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Class cls = new Class();
        BeanUtils.copyProperties(classAddNewDTO,cls);
        int row = mapper.insert(cls);
        if (row != 1){
            String message = "添加失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteClass(Long id) {
        ClassStanardVO byId = mapper.getById(id);
        if (byId == null){
            String message = "删除失败,所删除的数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int count = studentMapper.countByClassId(id);
        if (count != 0){
            String message = "删除失败,要删除的班级还有关联的学生信息";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        int row = mapper.deleteById(id);
        if (row != 1){
            String message = "删除失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateById(Long id,ClassUpdateDTO cls) {
        ClassStanardVO standard = mapper.getById(id);
        if (standard == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"修改失败未找到所修改数据");
        }
        Class clas = new Class();
        BeanUtils.copyProperties(cls,clas);
        clas.setId(id);
        int row = mapper.updateById(clas);
        if (row != 1){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"修改失败,系统繁忙");
        }

    }

    @Override
    public List<ClassListItemVO> classList() {
        return mapper.list();
    }

    @Override
    public ClassStanardVO getClassNameById(Long id) {
        ClassStanardVO cls = mapper.getById(id);
        if (cls == null){
            String message = "查询失败,所查询的数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return cls;
    }
}
